General={}
GeneralFunc={}
GeneralFunc.__index = GeneralFunc
setmetatable(General, GeneralFunc)
GeneralFunc.getGeneralCount = function(self, flag)
  local armys = UserData.userinfo.host.armyGroups
  local count = 0
  for k,v in pairs(armys) do
    if v.s==flag then count=count+1 end
  end
  return count
end
GeneralFunc.getGeneralsByLevel = function(self, flag)
  local armys = UserData.userinfo.host.armyGroups
  local sortedArmys = {}
  for k,v in pairs(armys) do
    sortedArmys[#sortedArmys+1] = {id=tonumber(k), level=v.l}
  end
  local sortFunc = function(a, b) return b.level < a.level end
  table.sort(sortedArmys, sortFunc)
  return sortedArmys
end
GeneralFunc.getGenerals = function(self, flag)
  local res = {}
  local armys = UserData.userinfo.host.armyGroups
  local sortedArmys = {}
  for k,v in pairs(armys) do
    if v.s==1 then
      sortedArmys[#sortedArmys+1] = {id=tonumber(k), level=v.l}
    end
  end
  local sortFunc = function(a, b) return b.level < a.level end
  table.sort(sortedArmys, sortFunc)
  --玩家
  for k,v in ipairs(sortedArmys) do
    if v.id==1 then res[#res+1]=v end
  end
  --上阵英雄
  local upArmys = {}
  local zxInfo = UserData.userinfo.host.formations[tostring(UserData.userinfo.host.defaultFormation)]
  if zxInfo~=nil then
    for k,v in pairs(zxInfo) do
      if k>1 and v>0 then
        upArmys[v] = 1
      end
    end
  end
  for k,v in ipairs(sortedArmys) do
    if v.id~=1 and upArmys[v.id]~=nil then res[#res+1]=v end
  end
  --其他
  for k,v in ipairs(sortedArmys) do
    if v.id~=1 and upArmys[v.id]==nil then res[#res+1]=v end
  end
  return res 
end

GeneralFunc.create = function(self, id)
  local a = {}
  setmetatable(a, GeneralFunc)
  a.generalInfo = UserData.userinfo.host.armyGroups[tostring(id)]
  a.generalConfig = UserData.config.models.General[id]
  return a
end
